package org.come.summonequip;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingUtilities;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.plaf.basic.BasicTreeUI;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreePath;

import org.cbg.until.TraslationDemoScrollBarUI;
import org.come.daily.JpanelDailytask;
import org.come.until.CutButtonImage;
import org.come.until.SrcollPanelUI;

import com.tool.btn.FormsOnOffBtn;
import com.tool.tcpimg.UIUtils;

public class JpanelHelpMain extends JPanel {

    private JTree jTree; // 活动列表
    private DefaultMutableTreeNode top; // 总节点

    private JScrollPane paneLeft, paneRight;

    public JpanelHelpMain() {
        setPreferredSize(new Dimension(471, 377));
        setOpaque(false);
        setLayout(null);

        // 关闭按钮事件
        FormsOnOffBtn offBtn = new FormsOnOffBtn("inkImg/hongmu/1/s74.png", 1, 93);
        offBtn.setBounds(490 - 37, 0, 25, 25);
        this.add(offBtn);

        getPaneLeft();
        getPaneRight();
    }

    private ImageIcon iconBack;

    @Override
    protected void paintComponent(Graphics g) {
        // TODO Auto-generated method stub
        super.paintComponent(g);
        if (iconBack == null) {
            iconBack = CutButtonImage.getImage("inkImg/hongmu/2/炼化规则说明_底板_w471,h337.png", 417, 336);
        }
        g.drawImage(iconBack.getImage(), 0, 0, 471, 377, this);
    }

    public ImageIcon getIconBack() {
        return iconBack;
    }

    public void setIconBack(ImageIcon iconBack) {
        this.iconBack = iconBack;
    }

    public JTree getjTree() {
        if (jTree == null) {
            // 套装列表
            jTree = new JTree(getTop());
            jTree.setOpaque(false);
            jTree.putClientProperty("JTree.lineStyle", "None"); // 去除jtree的线条
            ((BasicTreeUI) jTree.getUI()).setLeftChildIndent(0); // 设置父节点与子节点左对齐，并去除缩进
            DefaultTreeCellRenderer cellRenderer = new DefaultTreeCellRenderer() {
                @Override
                public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected,
                                                              boolean expanded, boolean leaf, int row, boolean hasFocus) {

                    DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
                    super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
                    // 判断是几级的叶子
                    return this;
                }
            };
            cellRenderer.setLeafIcon(null); // 设置叶子图标
            try {
                cellRenderer.setOpenIcon(CutButtonImage.cuts("skin/8100226/button/B108.png")[0]); // 设置打开子节点图标
                cellRenderer.setClosedIcon(CutButtonImage.cuts("skin/8100226/button/B109.png")[0]); // 设置关闭子节点图标
            } catch (Exception e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

            cellRenderer.setFont(UIUtils.TEXT_FONT1);// 设置字体.
            cellRenderer.setBackgroundNonSelectionColor(new Color(0, 0, 0, 0));// 设置非选定节点的背景色
            cellRenderer.setBackgroundSelectionColor(new Color(99, 93, 90));// 设置节点在选中状态下的背景色
            cellRenderer.setBorderSelectionColor(new Color(0, 0, 0, 0));// 设置选中状态下节点边框的颜色

            /*
             * 设置选时或不选时，文字的变化颜色
             */
            cellRenderer.setTextNonSelectionColor(Color.white);// 设置绘制未选中状态下节点文本的颜色
            cellRenderer.setTextSelectionColor(Color.white);// 设置绘制选中状态下节点文本的颜色
            jTree.setCellRenderer(cellRenderer);
            jTree.setRootVisible(false); // 隐藏根节点
            // jTree.setRowHeight(60); // 设置节点行高
            // 单击展开子节点
            jTree.addMouseListener(new MouseAdapter() { // 添加鼠标事件处理
                @Override
                public void mousePressed(MouseEvent e) {
                    if (SwingUtilities.isLeftMouseButton(e)) { // 点击了鼠标左键
                        TreePath path = jTree.getSelectionPath();
                        if (path != null) {
                            if (jTree.isExpanded(path)) {
                                jTree.collapsePath(path);// 关闭节点
                            } else {
                                jTree.expandPath(path);// 展开节点
                            }
                        }

                    }
                }
            });
            jTree.addTreeSelectionListener(new TreeSelectionListener() {

                @Override
                public void valueChanged(TreeSelectionEvent e) {

                    JTree tree = (JTree) e.getSource();
                    // 利用JTree的getLastSelectedPathComponent()方法取得目前选取的节点.
                    DefaultMutableTreeNode selectionNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();

                    if (selectionNode == null) {
                        return;
                    }
                    String nodeName = selectionNode.toString();
                    if (selectionNode.isLeaf()) {// 判断是否是叶子节点
                        if (selectionNode.getUserObject() instanceof JpanelDailytask) {
                            // visitAllNodes2(tree);
                            // dailytask.setBorder(BorderFactory.createLineBorder(Color.yellow));
                        }
                    } else {
                    }
                }
            });
        }
        return jTree;
    }

    public void setjTree(JTree jTree) {
        this.jTree = jTree;
    }

    public DefaultMutableTreeNode getTop() {
        if (top == null) {
            top = new DefaultMutableTreeNode("");
        }
        return top;
    }

    public void setTop(DefaultMutableTreeNode top) {
        this.top = top;
    }

    public JScrollPane getPaneLeft() {
        if (paneLeft == null) {
            paneLeft = new JScrollPane(getjTree());
            paneLeft.setVerticalScrollBarPolicy(22);
            paneLeft.getVerticalScrollBar().setUI(new TraslationDemoScrollBarUI(this.paneLeft));
            paneLeft.getViewport().setOpaque(false);
            paneLeft.setOpaque(false);
            paneLeft.setBounds(27, 73, 135, 250);
            paneLeft.setBorder(BorderFactory.createEmptyBorder());
            paneLeft.setHorizontalScrollBarPolicy(31);
            this.add(paneLeft);
        }
        return paneLeft;
    }

    public void setPaneLeft(JScrollPane paneLeft) {
        this.paneLeft = paneLeft;
    }

    public JScrollPane getPaneRight() {
        if (paneRight == null) {
            paneRight = new JScrollPane();
            paneRight.setVerticalScrollBarPolicy(22);
            paneRight.getVerticalScrollBar().setUI(new TraslationDemoScrollBarUI(this.paneLeft));
            paneRight.getViewport().setOpaque(false);
            paneRight.setOpaque(false);
            paneRight.setBounds(166, 73, 275, 250);
            paneRight.setBorder(BorderFactory.createEmptyBorder());
            paneRight.setHorizontalScrollBarPolicy(31);
            this.add(paneRight);
        }
        return paneRight;
    }

    public void setPaneRight(JScrollPane paneRight) {
        this.paneRight = paneRight;
    }

}
